import pandas as pd
import os
import statsmodels.api as sm
from sklearn.metrics import mean_squared_error
from math import sqrt
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf
from statsmodels.tsa.stattools import adfuller

dateparse = lambda date: pd.to_datetime(date, format='%Y-%m-%d')
clientData = pd.read_excel(os.getcwd() + "\\data\\1647848272130494.xlsx", index_col='缴费日期', parse_dates=['缴费日期'],
                           date_parser=dateparse)
ClientData = clientData.loc[clientData["用户编号"] == 1000000001]

train = ClientData[:]
test = ClientData[:]
ts_ARIMA = train['缴费金额（元）'].astype('float64')
fit1 = sm.tsa.ARIMA(ts_ARIMA, order=(1, 0, 1)).fit()
data_len = ts_ARIMA.count() - 1
y_hat_ARIMA = fit1.predict(start=0, end=data_len, dynamic=False)
rmse = sqrt(mean_squared_error(test['缴费金额（元）'], y_hat_ARIMA.to_frame()))

plt.plot(y_hat_ARIMA, label=rmse)
plt.legend(loc='best')

diff1 = ClientData['缴费金额（元）'].diff().dropna()
plot_acf(diff1)
yarn_result = adfuller(ClientData['缴费金额（元）'], 1, autolag='AIC')
yarn_result2 = adfuller(ClientData['缴费金额（元）'], 1, autolag='AIC')
print('1阶差分前 ADF: %f' % yarn_result2[0])
print('1阶差分后 p value: %f' % yarn_result2[1])
print('差分前 ADF: %f' % yarn_result[0])
print('差分前 p value: %f' % yarn_result[1])
plt.show()